// pages/mingshitang/index.js
var app = getApp()
var c = require("../common.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typestatus: 3,
    yuyuestyle: 0,
    renqistyle: 0,
    page: 0,
    qid: 1,
    curtopid: 0,
    yuyue: 0,
    renqi: 0,
    twoid: 0,
    classname: "全部"
  },
  //跳转到对应的店铺
  tomyshop(e) {
    wx.navigateTo({
      url: '../myshop/index?class_id=' + e.currentTarget.dataset.classid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //加载顶部菜单并将第一个菜单的店铺列出来
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/gettopmenu",
      {},
      res => {
        this.setData({
          topcate: res.data,
          curtopid: res.data[0]["cate_id"],
        })
        this.changetwocate(res.data[0]["cate_id"])
        this.getallclassbyid(res.data[0]["cate_id"])
      }
    )
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getslide",
      {},
      res => {
        this.setData({
          slide: res.data
        })
      },
      function (res) {

      }
    )
  },
  //改变当前顶部菜单状态
  changetopcate(e) {
    var data = c.getdata(e)
    this.setData({
      curtopid: data.cateid,
      classname: "全部",
      page:0
    })
    this.changetwocate(data.cateid)
    this.getallclassbyid(data.cateid)
  },
  //根据上级id获取下级id 
  changetwocate(pid) {
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/gettwocate",
      {
        pid: pid
      },
      re => {
        this.setData({
          twocate: re.data
        })
      }
    )
  },
  //改变二级分类id值
  changetwoid(e) {
    var typestatus = this.data.typestatus;
    this.setData({
      twoid: this.data.twocate[e.detail.value].cate_id,
      classname: this.data.twocate[e.detail.value].name.substr(0, 2)
    })
    this.getallclassbyid(this.data.twocate[e.detail.value].cate_id)
  },
  //通过id获取所有店铺列表
  getallclassbyid(id) {
    var typestatus = this.data.typestatus;
    wx.showLoading({
      title: '加载中...',
    })
    var page = this.data.page;
    this.setData({ page: page + 1 });

    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getallclassbyid",
      { id: id, page: page, typestatus: typestatus },
      res => {
        console.log(res);
        this.setData({
          allclass: res.data
        })
        setTimeout(res => {
          wx.hideLoading()
        }, 500)
      }
    )
  },
  //人气排行
  changerenqi() {
    var typestatus = this.data.typestatus;
    var id = this.data.curtopid;
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({ renqistyle: 1 });
    this.setData({ yuyuestyle: 0 });
    this.setData({ yuyue: 0 });
    this.setData({ page: 1 });
    var page = 0;
    var renqi = this.data.renqi;
    if (renqi == 1) {
      this.setData({ renqi: 2 });
    } else {
      this.setData({ renqi: 1 });
    }
    var renqi = this.data.renqi;
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getallclassbyid",
      { id: id, page: page, renqi: renqi, typestatus: typestatus },
      res => {
        this.setData({
          allclass: res.data
        })
        setTimeout(res => {
          wx.hideLoading()
        }, 500)
      }
    )

  },
  //预约排行
  changeyuyue() {
    var typestatus = this.data.typestatus;
    var id = this.data.curtopid;
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({ renqistyle: 0 });
    this.setData({ yuyuestyle: 1 });
    this.setData({ renqi: 0 });
    this.setData({ page: 1 });
    var page = 0;
    var yuyue = this.data.yuyue;
    if (yuyue == 1) {
      this.setData({ yuyue: 2 });
    } else {
      this.setData({ yuyue: 1 });
    }
    var yuyue = this.data.yuyue;

    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getallclassbyid",
      { id: id, page: page, yuyue: yuyue, typestatus: typestatus },
      res => {
        this.setData({
          allclass: res.data
        })
        setTimeout(res => {
          wx.hideLoading()
        }, 500)
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toviewlog() {
    wx.navigateTo({
      url: '../viewlog/index',
    })
  },
  tonewslist() {
    wx.navigateTo({
      url: '../newslist/index',
    })
  },
  qiehuanlist: function (e) {
    var qid = e.currentTarget.dataset.qid;
    this.setData({ qid: qid });
  },
  moreinfo: function () {
    var typestatus = this.data.typestatus;
    var id = this.data.curtopid;
    var page = this.data.page;
    var yuyue = this.data.yuyue;
    var renqi = this.data.renqi;
    this.setData({ page: page + 1 });
    var allclassold = this.data.allclass;
    console.log(id); console.log(page); console.log(yuyue); console.log(renqi);
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/getallclassbyid",
      { id: id, page: page, yuyue: yuyue, renqi: renqi, typestatus: typestatus },
      res => {
        var all = allclassold.concat(res.data);
        console.log(all);
        this.setData({
          allclass: all
        })
        setTimeout(res => {
          wx.hideLoading()
        }, 500)
      }
    )

  },
  inputclick: function () {
    wx.navigateTo({
      url: '/pages/search/search',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})